import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
/* 1. 引入 store */
// import store from "../store/index"

Vue.use(VueRouter)
let originPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(path){
  return originPush.call(this,path).catch(()=>{})
}

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/layout',
    name:'Layout',
    component: () => import("../components/Layout.vue"),
    children:[
      {
        path:'',
        name:'Home',
        component:() => import("../components/Home.vue")
      },
      {
        path:'/grademana',
        name:'grademana',
        component:() => import("../components/GradeManagement.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"等级考试",key:2,path:"/grademana"},{text:"成绩管理",key:3,path:"grademana"}]
       },
      {
        path:'/release',
        name:'release',
        component:() => import("../components/Release.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"等级考试",key:2,path:"/release"},{text:"考试发布",key:3,path:"release"}]
      },
      {
        path:'/rebuild',
        name:'Rebuild',
        component:() => import("../components/Rebuild.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"免修重修",key:2,path:"/rebuild"},{text:"重修名单",key:3,path:"rebuild"}]
      },
      {
        path: '/audit',
        name: "Audit",
        component: () => import("../components/SignReview.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"等级考试",key:2,path:"/audit"},{text:"报名审核",key:3,path:"audit"}]
      },
      {
        path: '/applyFor',
        name: "ApplyFor",
        component: () => import("../components/ApplyFor.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"免修重修",key:2,path:"/applyFor"},{text:"免修申请",key:3,path:"applyFor"}]
      },
      {
        path: '/registration',
        name: "registration",
        component: () => import("../components/registration.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"等级考试",key:2,path:"/registration"},{text:"网上报名",key:3,path:"registration"}]
      },
      {
        path: '/uda',
        name: "uda",
        component: () => import("../components/Uda.vue"),
        meta:[{text:"教务系统",key:1,path:"/Layout"},{text:"免修重修",key:2,path:"/uda"},{text:"免修审核",key:3,path:"uda"}]
      },
    ]
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
/* 1. 全局前置导航守卫 
  登录校验
  1.如何没有登录情况 /
  2. 如果用户有登录情况 直接去到允许跳转的地方
*/
/* 全局前置的导航 */
function getCookie(key){
  let str = "";
  var tmp = document.cookie.split('; ');
  for(var i=0;i<tmp.length;i++){
      var t = tmp[i].split('=');
      if(t[0]===key){
          str = t[1];
      }
  }
  return str;   
}

// router.beforeEach((to,from,next)=>{
//   if(store.state.status === 1 || to.path === "/"){
//     next();// 顺利进入到下一个 Path中
//   }else{
//     next("/");
//   }
// })

router.beforeEach((to,from,next)=>{
  const username = getCookie("username");
  // console.log(getCookie('username'));
  if( username || to.path === "/"){
    next();// 顺利进入到下一个 Path中
  }else{
    next("/");
  }
})



export default router
